

var newer = newer || {};
newer.gd = newer.gd || {};

newer.FriendsPostsApp = function(o) {
	this.contentDivId = "friends-posts-content-div";
	this.title = "friends";
	this.tmp = new Object();
};

newer.FriendsPostsApp.prototype = {
	init: function() {
		$("#" + this.contentDivId).html('<div id="posts-of-friends-div"><div class="app-title-class">Check out what others are doing</div></div>');
		this.update();
	},
	
	update: function() {
		this.updateCreatePost();
		this.updatePosts();
	},
	
	updateCreatePost: function() {
		var self = this;
		$("#posts-of-friends-div").append('<div id="post-create-div"></div>')
		$("#post-create-div").append("<div id='home-post-create-div' class='home-post-create-class'>Share what's new</div>");
		$("#post-create-div").append('<div id="home-post-textarea-div" style="display:none"></div>');
		
		$("#home-post-textarea-div").append('<textarea name="post-create-content" rows="8" cols="60" id="post-create-content" class="home-post-create-class"></textarea>');
		$("#home-post-textarea-div").append('<div id="post-create-cross"></div>');
		$("#home-post-textarea-div").append('<div id="post-create-share-area"></div>');
		$("#post-create-share-area").append('<div id="post-create-share-div" class="link-like-element post-create-share-buttons post-create-share-div-disable">Share</div>');
		
		$("#post-create-share-area").append('<div id="post-create-share-dropdown"></div>');
		this.sdd = new newer.PostShareDropDown({root:"post-create-share-dropdown", preset:"all",cp:this});	
		
		$("#home-post-create-div").on("click", function() {
			$("#home-post-create-div").hide();
			$("#home-post-textarea-div").slideDown('fast');
			$("textarea#post-create-content").focus();
		});
		
		$("#post-create-cross").html('<img src="/public/images/cross.png" width="30px"/>').addClass("link-like-element").on("click", function() {
			$("textarea#post-create-content").val("");
			$("#home-post-create-div").show();
			$("#home-post-textarea-div").hide();
		});
		
		$("#post-create-content").on("keyup", function(event) {
			if($("textarea#post-create-content").val().length > 0 && $("#post-create-share-div").hasClass("post-create-share-div-disable")) {
				$("#post-create-share-div").removeClass("post-create-share-div-disable").addClass("post-create-share-div-enable");
			} else if($("textarea#post-create-content").val().length == 0 && $("#post-create-share-div").hasClass("post-create-share-div-enable")) {
				$("#post-create-share-div").removeClass("post-create-share-div-enable").addClass("post-create-share-div-disable");
			}
		});
		
		$("#post-create-share-div").on("click", function() {
			if($("#post-create-share-div").hasClass("post-create-share-div-enable")) {
				self.sdd.generateRes();
				$.debug(self.sdd.result);
				$.post("/Posts/createPostJSON", {
					"content":$("textarea#post-create-content").val(),
					"uid":newer.gd.user.id
				}, function(post) {
					$("#posts-list-div").prepend('<div id="friend-post-'+post.id+'" style="display:none;"></div>');
					new newer.PostDisplay({
						domNode:$("#friend-post-"+post.id),
						idSuffix:post.id,
						postInfo:post
					});
					$("#friend-post-"+post.id).slideDown("slow");
				}, "json");
				$("textarea#post-create-content").val("");
				$("#home-post-create-div").show();
				$("#home-post-textarea-div").hide();
			}
		});
	},
	
	updatePosts: function() {
		var self = this;
		$("#posts-list-div").remove();
		$("#posts-of-friends-div").append('<div id="posts-list-div"></div>');
		$.post("/Posts/getFriendsPostsByUserId?uid=" + newer.gd.user.id, {}, function(posts) {
			$.each(posts, function(index, post) {
				$("#posts-list-div").append('<div id="friend-post-'+post.id+'"></div>');
				new newer.PostDisplay({
					domNode:$("#friend-post-"+post.id),
					idSuffix:post.id,
					postInfo:post
				});
			})
		}, "json");
	},
	
	getIcon: function() {
		return '<div><img src="/public/images/green_35.png" style="width:55px;height:55px;" title="Friends\' Posts"/></div>';
	},
	
	getContent: function() {
		return '<div id="'+this.contentDivId+'"></div>';
	}
};
